/*
   Copyright (c) 2023 李伟国
   learner-platform is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
               http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details. 
*/

package cn.platform.email;

import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
@Component
public class SendEmail {
    // 简单邮件
    //授权码
    static String AuthorizationCode="uxthobfrhtlhdijd";


    //发送一封简单的邮件
    public void sendInformMail(String authCode, String mailAddress) throws Exception {
        MailProperties mailProperties = new MailProperties();
        String password = mailProperties.getPassword();
        if(mailAddress==null||mailAddress.equals("")){
            return;
        }
        Properties prop = new Properties();
        //设置QQ邮件服务器
        prop.setProperty("mail.host","smtp.qq.com");
        //邮件发送协议
        prop.setProperty("mail.transport.protocol","smtp");
        //需要验证用户名密码
        prop.setProperty("mail.smtp.auth","true");

        //QQ邮箱的特别设置
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.put("mail.smtp.ssl.enable","true");
        prop.put("mail.smtp.ssl.socketFactory",sf);

        //使用JavaMail发送邮件的5个步骤

        //1、创建定义整个应用程序所需的环境信息的Session对象
        Session session = Session.getDefaultInstance(prop, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("2590863120@qq.com","uxthobfrhtlhdijd");
            }
        });
        //开启session的debug模式

        //2、通过session得到transport对象
        Transport ts = session.getTransport();
        //3、使用邮箱的用户名和授权码连上邮件服务器
        ts.connect("smtp.qq.com","2590863120@qq.com","uxthobfrhtlhdijd");


        //4、创建邮件
        Message message = new MimeMessage(session);
        //指明邮件的发件人
        message.setFrom(new InternetAddress("2590863120@qq.com"));
        //指明邮件的收件人
        message.setRecipient(Message.RecipientType.TO,new InternetAddress(mailAddress));
        message.setSubject("选课通知");   //邮件主题
        //邮件的内容
        message.setContent("<h1>验证码</h1>" +
                "<h2>您的验证码为："+authCode+"</h2>" +
                "<h2>十分钟内有效，请勿泄露</h2>" +
                "<br/><br/><br/>" +
                "<p>此为选课系统发送，请勿回复</p><p>选课系统</p>","text/html;charset=UTF-8");


        //5、发送邮件

        ts.sendMessage(message, message.getAllRecipients());

        //6、关闭连接
        ts.close();
    }
}
